﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace K3R.SqlLogging {
	public class LogConfig : ConfigurationSection {
		[ConfigurationProperty("source", IsRequired = true)]
		public string Source {
			get {
				return (string)base["source"];
			}
			set {
				base["source"] = value;
			}
		}

		[ConfigurationProperty("connectionStringName")]
		public string ConnectionStringName {
			get {
				return (string)base["connectionStringName"];
			}
			set {
				base["connectionStringName"] = value;
			}
		}

		[ConfigurationProperty("logAllEvents", DefaultValue = false)]
		public bool LogAllEvents {
			get {
				return (bool)base["logAllEvents"];
			}
			set {
				base["logAllEvents"] = value;
			}
		}
	}

	public static class Settings {
		static Settings() {}
		private static LogConfig _settings = ConfigurationManager.GetSection("k3r.sqlLogging") as LogConfig;

		public static string Source {
			get {
				string source = null;

				if (_settings != null)
					source = _settings.Source;

				return source;
			}
		}

		public static string ConnectionString {
			get {
				string connectionStringName = null;
				string connectionString = null;

				if (_settings != null)
					connectionStringName = _settings.ConnectionStringName;

				if (connectionStringName != null && connectionStringName != "")
					connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
				else connectionString = K3R.Settings.ConnectionString;

				return connectionString;
			}
		}

		public static bool LogAllEvents {
			get {
				bool logAllEvents = false;

				if (_settings != null)
					logAllEvents = _settings.LogAllEvents;

				return logAllEvents;
			}
		}
	}
}
